package com.example.guiguiapplicatio.fragment.Fulfillment.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.FulfillmentservicedetailData;

import java.util.ArrayList;
import java.util.List;

public class FulfillmenthomeAdapter extends RecyclerView.Adapter<FulfillmenthomeAdapter.FulfillmenthomeViewHolder>{
    private List<Integer> selectedPositions = new ArrayList<>();
    private int mLayoutId;
    private Context mContext;

    private List<FulfillmentservicedetailData> ServicePackageitemsList;
    public FulfillmenthomeAdapter(List<FulfillmentservicedetailData> servicePackageitemsList, Context context) {
        this.ServicePackageitemsList = servicePackageitemsList;
        mContext = context;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FulfillmenthomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fulfillment, parent, false);
        return new FulfillmenthomeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FulfillmenthomeViewHolder holder, @SuppressLint("RecyclerView") int position) {
        FulfillmentservicedetailData serviceitems =ServicePackageitemsList.get(position);
        Log.d("11111111111111111111111111", String.valueOf(ServicePackageitemsList.size()));
        if(serviceitems.getServicePackname() != null || serviceitems.getServiceitemsList() != null){
            holder.headwriting1TextView.setText(serviceitems.getServicePackname());
            double ratio;
            if (serviceitems.getAllTotal() != 0) {
                ratio = (double) serviceitems.getWorkTotal() / serviceitems.getAllTotal() * 100;
            } else {
                ratio = 0; // Or any other value you prefer when the denominator is zero
            }
            String ratioText = String.format("%.2f%%", ratio);
            holder.headwriting3TextView.setText(ratioText);
            LinearLayoutManager manager = new LinearLayoutManager(mContext);
            manager.setOrientation(LinearLayoutManager.VERTICAL);
            holder.recyclerView.setLayoutManager(manager);
            holder.recyclerView.setAdapter(new FulfillmentitemsAdapter(serviceitems.getServiceitemsList(), R.layout.item_fulfillment_detailed,mContext));
        }
    }



    public void setData(List<FulfillmentservicedetailData> data, int mlayoutId) {
        if (data == null) {
            data = new ArrayList<>();
        }
        this.ServicePackageitemsList = data;
        this.mLayoutId = mlayoutId;
    }
    @Override
    public int getItemCount() {
        return ServicePackageitemsList.size();
    }

    public static class FulfillmenthomeViewHolder extends RecyclerView.ViewHolder {
        TextView headwriting1TextView;
        TextView headwriting3TextView;
        RecyclerView recyclerView;

        public FulfillmenthomeViewHolder(@NonNull View itemView) {
            super(itemView);
            headwriting1TextView = itemView.findViewById(R.id.head_writing1);
            recyclerView = (RecyclerView) itemView.findViewById(R.id.service_service_list);
            headwriting3TextView = itemView.findViewById(R.id.head_writing3);
        }
    }
}
